package com.example.li_han.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DataUtils {
    //连接数据库操作
    public static Connection getConn() throws ClassNotFoundException {
        Connection conn = null;
        //核心代码

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/news?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false",
                    "root",""
                    );
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return conn;
    }
    //关闭连接
    public static void closeConnection (Connection conn) {
        try {
            conn.close();//关闭数据库链接
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


    public static void main(String[] args) {
        Connection conn = ConnUtils.getConn();
        if(conn!=null){
            System.out.println("成功");
        }else {
            System.out.println("失败");
        }
        //关闭连接
        DataUtils.closeConnection(conn);
    }
}
